package com.company.cloud.club.modules.controller;

import com.company.cloud.club.modules.constants.ClubMemberType;
import com.company.cloud.club.modules.constants.CollectionType;
import com.company.cloud.club.modules.core.dto.Result;
import com.company.cloud.club.modules.core.util.ValidateUtil;
import com.company.cloud.club.modules.db.dao.ClubMemberMapper;
import com.company.cloud.club.modules.db.domain.ClubMember;
import com.company.cloud.club.modules.db.domain.Collection;
import com.company.cloud.club.modules.dto.collection.CollectionBo;
import com.company.cloud.club.modules.service.ClubMemberService;
import com.company.cloud.club.modules.service.CollectionService;
import com.company.cloud.club.modules.utils.LoginContextUtils;
import com.company.cloud.common.auth.annotation.Login;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * *****************************************************************************
 * <p>
 * 功能名           ：com.company.cloud.club.modules.controller
 * 系统名           ：
 * <p>
 * *****************************************************************************
 * Modification History
 * <p>
 * Date        Name                    Reason for Change
 * ----------  ----------------------  -----------------------------------------
 * 2021/9/4     zhangxu                 Created
 */
@Slf4j
@RestController
@Api(tags = "CollectionController")
@RequestMapping("/coll")
public class CollectionController {

    @Resource
    private CollectionService collectionService;

    @Autowired
    private ClubMemberService clubMemberService;
    
    @ApiOperation("收藏列表")
    @GetMapping(value = "/list")
    public Result getCollectionList(@RequestParam("type") String type) {
        Integer currentUserId = LoginContextUtils.getUserId();
        List<Collection> list = collectionService.getCollectionList(currentUserId, type);
        return Result.ok(list);
    }

    @ApiOperation("收藏")
    @PostMapping(value = "/create")
    public Result create(@RequestBody CollectionBo bo) {
        Integer currentUserId = LoginContextUtils.getUserId();

        String type = bo.getType();
        String resId = bo.getResId();

        if ("circle".equals(type)) {
            ClubMember member = clubMemberService.selectByClubIdAndUserId(resId, currentUserId);
            if (ValidateUtil.isEmpty(member)) {
                member = new ClubMember(0);
                member.setClubId(resId);
                member.setType(ClubMemberType.MEMBER);
                member.setUserId(currentUserId);
                boolean flag = clubMemberService.save(member);
                if (flag) {
                    return Result.ok("关注成功", member.getId());
                }
                return Result.ok("关注失败", "");
            } else {
                if (!ClubMemberType.MEMBER.equals(member.getType())) {
                    return Result.ok("圈主或圈理事无法取消", "");
                }
                clubMemberService.removeMember(resId, currentUserId);
                return Result.ok("取消关注", member.getId());
            }
        }

        Collection param = collectionService.getByResId(type, resId, currentUserId);
        if (ValidateUtil.isEmpty(param)) {
            param = new Collection(0);
            param.setName(bo.getName());
            param.setPageUrl(bo.getPageUrl());
            param.setResId(resId);
            param.setType(type);
            param.setUserId(currentUserId);
            boolean flag = collectionService.save(param);
            if (!flag) {
                return Result.failed("收藏失败");
            }
            if (CollectionType.MEMBER.equals(type)) {
                ClubMember member = new ClubMember(0);
                member.setClubId(resId);
                member.setType(ClubMemberType.MEMBER);
                member.setUserId(currentUserId);
                boolean memberFlag = clubMemberService.save(member);
                if (!memberFlag) {
                    return Result.failed("关注失败");
                }
            }
            return Result.ok(param.getId());
        } else {
            collectionService.removeByResId(param.getId());
            return Result.ok("取消收藏", param.getId());
        }

    }

}